import os
import datetime

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect

# Create your views here.
from django.template.loader import render_to_string
from django.urls import reverse
from django.views.generic import TemplateView

from my_project.settings import BASE_DIR

class Cat(object):
    def display(self):
        return '小一'

def HelloWorld(request):
    print(reverse("HelloWorld"))
    return HttpResponse('Hello World')

def HelloCH(request):
    return HttpResponse('Hello CH')

def article_list(request,month):

    return HttpResponse('article:{}'.format(month))

def search(request):
    name=request.GET.get('WRITER','HT')
    print(name)

    return HttpResponse('查询成功')

cat=Cat()

def html(request):
    username = '张三'
    age = 16
    image_url = '/media/t.jpg'
    user_lists=[
        {'username': '小一','age': 12},
        {'username': '小二', 'age':13},
    ]
    return render(request,'index.html', {
        'username': username,
        'age': age,
        'image_url': image_url,
        'user_lists': user_lists,
        'cat': cat
    })

def http_response(request):
    print(request.method)
    resp=HttpResponse('响应成功')
    return resp

def no_data_404(request):
    return HttpResponse('404')

def article_detail(request,article_id):
    if article_id<1000:
        return redirect(reverse('no_data_404'))
    return HttpResponse('文章{}内容'.format(article_id))

def tag(request):
    '''DTL的标签练习'''
    user_lists = [
        {'username': '一一', 'age': 12, 'sex': '女'},
        {'username': '二二', 'age': 23},
        {'username': '三一', 'age': 22},
        {'username': '三二', 'age': 31},
    ]
    return render(request,'tag.html', {
        'user_lists': user_lists,

    })
def test_filter(request):
    '''过滤器的使用'''
    name='china'
    user_lists=[]
    user_info = None
    pi = 3.1415926
    mdate=datetime.datetime(2020, 1, 1)
    html='<h3>过滤器使用练习</h3>'
    return render(request,'test_filter.html', {
        'name': name,
        'user_lists' :user_lists,
        'user_info': user_info,
        'pi': pi,
        'mdate': mdate,
        'html': html,
    })
def mine_filter(request):
    '''自定义过滤器的使用'''
    username='张三'
    pnumber='18811718763'

    return render(request,'mine_filter.html', {
        'username': username,
        'pnumber': pnumber

    })
# class HomeView(TemplateView):
#     template_name = 'index.html'